package nusweektracker;

import java.util.Calendar;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

public class Logic {
	
	private LinkedList<String> outputs = new LinkedList<String>();
	
	
	public LinkedList<String> getOutputs() {
		return outputs;
	}
	public void calculateCurrentWeek(int year, int month, int date) {
		Calendar todayDate = Calendar.getInstance();
		Calendar firstWeek = Calendar.getInstance();
		firstWeek.set(year, month - 1, date);
		// get the day of the week

		todayDate.set(Calendar.DAY_OF_MONTH, todayDate.get(Calendar.DAY_OF_MONTH)
				- todayDate.get(Calendar.DAY_OF_WEEK) + 2);
		firstWeek.set(Calendar.DAY_OF_MONTH, firstWeek.get(Calendar.DAY_OF_MONTH)
				- firstWeek.get(Calendar.DAY_OF_WEEK) + 2);

		output("Start week:\t"+ todayDate.getTime().toString());
		output("End week:\t" + firstWeek.getTime().toString());

		long differenceInMs = todayDate.getTimeInMillis()
				- firstWeek.getTimeInMillis();

		int weeks = (int) (differenceInMs / ((double) 1000 * 60 * 60 * 24 * 7));
		output("Duration (Nbr of weeks) :" + weeks);
	}

	/**
	 * @param daysToAdd
	 * @return
	 */
	public void addDays(int daysToAdd) {
		addDays(0, 0, 0, daysToAdd, true);
	}

	public void addDays(int year, int month, int date, int daysToAdd) {
		addDays(year, month, date, daysToAdd, false);
	}

	public void addDays(int year, int month, int date, int daysToAdd,
											boolean isUsingCurrentDate) {
		Calendar timeToReturn = Calendar.getInstance();
		Calendar today = (Calendar) timeToReturn.clone();
		if (!isUsingCurrentDate) {
			timeToReturn.set(year, month-1, date);
		}

		long daysToAddInMs = TimeUnit.MILLISECONDS.convert((long) daysToAdd, TimeUnit.DAYS);
		timeToReturn.setTimeInMillis(timeToReturn.getTimeInMillis()
				+ daysToAddInMs);
		
		
		if (isUsingCurrentDate) {
			output("add [" + daysToAdd + "]days from "+today.getTime()+ " ");
			output("results : " + timeToReturn.getTime().toString());

		} else if (!isUsingCurrentDate) {
			output("add [" + daysToAdd + "]days from " + year +

			"/" + month + "/" + date + " result: " + timeToReturn.getTime().toString());
		}
	}

	public void output(String line) {
		outputs.add(line);

	}

	public static void main(String[] args) {
		Logic logic = new Logic();
		
		//nbr of weeks since week 1 academic week.
		logic.calculateCurrentWeek(2013, 8, 12);
		logic.addDays(35);

		logic.addDays(2013, 10, 23, 25);
		
		while (!logic.getOutputs().isEmpty()) {
			System.out.println(logic.getOutputs().poll());
		}
	}

}
